﻿<?php


if(!empty($_POST)){
             //实例化数据类
    if($_POST['code']==$_SESSION['code']&&!empty($_POST['code'])){
			  $obj=new mysql($config);
			  $username=$_POST['username'];
		      

			  $password=((!empty($_COOKIE['password']))&&($_POST['password']==$_COOKIE['password']))?$_COOKIE['password']:md5($_POST['password']);


			  $sql="select admin.*,role.author_id from `admin` inner join `role` on admin.role_id=role.id where `username`='$username' and `password`='$password'";
			  //通过使用该函数返回查询的值，有则返回数据(true)，没有则返回空（false）
			  $date=$obj->getOne($sql);

			  if($date){
		           //判断当前是否点击记住我，如果选中则$_POST['checkbox']有值并且设置cookie值，否则为空并销毁cookie值
			  	   if(!empty($_POST['checkbox'])){

		            //设置cookie的名字，值和生命周期
			            setcookie('username',$date['username'],time()+3600*24);
			            setcookie('password',$date['password'],time()+3600*24);
			  	   }else{
			            setcookie('username','',0);
			            setcookie('password','',0);
			  	   }
		            
		           //如果当前选中了自动登录，则设置其值和生命周期存入cookie中
			  	   if(@$_POST['auto']){
			  	   	  setcookie('auto','1',time()+3600*7);
			  	   }else{
			  	   	   setcookie('auto','',0);
			  	   }


			  	    $_SESSION['admin']=$date;
			  	    $id=$_SESSION['admin']['author_id'];
					$sql="select url from author where id in($id)";
					$date=$obj->getCondition($sql);
					$arr=array();
					foreach ($date as $k => $v) {
						if($v['url']!=null){$arr['url'][]=$v['url'];}
					}
					 $_SESSION['admin']['author']=$arr;
					
			       echo '<script>alert("登录成功");location.href="index.php?c=index&a=index&mod=admin";</script>';

			  }else{

			  	  //假如用户名和密码都错，则取消记住密码和自动登录选择
			  	  setcookie('auto','',0);
			  	  setcookie('username','',0);
		          setcookie('password','',0);
			  	  echo '<script>alert("密码或用户名出错");history.go(-1);</script>';
			  }


	}
	else{
		echo '<script>alert("验证码有误");history.go(-1);</script>';
	}
}





require_once (VIEW_PATH.'admin/admin/login.html');



